﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FastReport;
using FastReport.Export.Pdf;
using FastReport.Export.Image;
using Newtonsoft.Json;
using System.IO;

namespace GenerateFastReport
{
    public partial class Form1 : Form
    {
        private AtriumPara para = new AtriumPara();
        private DataSet FDataSet = new DataSet();
        public Form1(string model)
        {
            InitializeComponent();
            CreateDataSet();
            if (model == "0") //打印预览
            {
                showPrintView();
                return;
            }
            else if (model == "1")
            {
                SavePDF();
                return;
            }
        }

        private void CreateDataSet()
        {

            string jsonpath = "Atrium.json";
            para = DeserializeJsonToObject<AtriumPara>(jsonpath);


        }
        /// <returns>对象实体</returns>
        public T DeserializeJsonToObject<T>(string json) where T : class
        {
            //StreamReader sread = File.OpenText(json);
            StreamReader sread = new StreamReader(json, Encoding.Default);
            JsonSerializer serializer = new JsonSerializer();
            StringReader sr = new StringReader(sread.ReadToEnd());
            object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
            T t = o as T;
            return t;
        }

        public class AtriumPara
        {
            public AtriumPara()
            {

                Name = "test";
                Age = "30";
                doctor = "hnyd";
                doctorsign = "";
                PatientInfo = "0"; //打印的病人信息
                HeartBeat = "60";  //心率 
                Atrium_Consluion = "左右心房时限正常，无传导阻滞的异常；\r\n左右心房形态正常，无肥大异常。";  //结论 

                P_LEFT = "0.1";
                P_RIGHT = "0.2";
                P_FOR = "0.3";
                P_BACK = "0.4";
                P_UP = "0.5";
                P_DOWN = "0.7";
                P_SPACE = "1";
                P_VCG_Duration = "110"; //P环时限
                P_VCG_HeatRate = "0";
                P_VCG_Conclusion = "大致正常心电图。";

                P_H_ROT = "0"; // 
                P_H_MAX = "0"; // 
                P_H_DEG = "0"; // 
                P_H_AREA1 = "0"; // 
                P_H_AREA2 = "0"; // 
                P_H_AREA3 = "0"; // 
                P_H_AREA4 = "0"; // 
                P_RS_ROT = "0"; // 
                P_RS_MAX = "0"; // 
                P_RS_DEG = "0"; // 
                P_RS_AREA1 = "0"; // 
                P_RS_AREA2 = "0"; // 
                P_RS_AREA3 = "0"; // 
                P_RS_AREA4 = "0"; // 
                P_F_ROT = "0"; // 
                P_F_MAX = "0"; // 
                P_F_DEG = "0"; // 
                P_F_AREA1 = "0"; // 
                P_F_AREA2 = "0"; // 
                P_F_AREA3 = "0"; // 
                P_F_AREA4 = "0"; // 

                P_Frank_Heartbeat = "70"; //  心率s
                P_Frank_Druation = "110"; //  房波时限
                P_Frank_Morphology = "形态正常"; //  形态被描述
                P_Frank_Conclusion = "xin"; //  结论

                P_Wilson_Heartbeat = "69"; //  心率
                P_Wilson_Druation = "100"; //  房波时限
                P_Wilson_RA_LA = "30"; //  右左房时限
                P_Wilson_LA = "80"; //  左房时限
                P_Wilson_PR = "210"; //   pr间期
                P_Wilson_LA_V = "180"; //   LAV间期
                P_Wilson_PStartLead = "Ⅱ"; //  p波起点最早导联
                P_Wilson_LA_PEndLead = "0"; //   p波结束最晚导联
                P_Wilson_Conclusion = "P波时限、幅值均正常。"; //  结论

                Fastreport_demo = "Atrium.frx";//   fastreport 模板路径
                Report_file = "test.pdf";// 生成报告的路径 
                PrintTime = DateTime.Now.ToString("yyyy-MM-dd  HH:mm:ss");

            }
            public string Name { set; get; }
            public string Age { set; get; }
            public string doctor { set; get; }
            public string doctorsign { set; get; }
            public string PatientInfo { get; set; } //打印的病人信息
            public string HeartBeat { set; get; }  //心率 
            public string Atrium_Consluion { set; get; }  //结论 
            //p环参数
            public string P_LEFT { set; get; }
            public string P_RIGHT { set; get; }
            public string P_FOR { set; get; }
            public string P_BACK { set; get; }
            public string P_UP { set; get; }
            public string P_DOWN { set; get; }
            public string P_SPACE { set; get; }
            public string P_VCG_Duration { set; get; } //P环时限
            public string P_VCG_HeatRate { set; get; }
            public string P_VCG_Conclusion { set; get; }
            //H面
            public string P_H_ROT { set; get; } // 
            public string P_H_MAX { set; get; } // 
            public string P_H_DEG { set; get; } // 
            public string P_H_AREA1 { set; get; } // 
            public string P_H_AREA2 { set; get; } // 
            public string P_H_AREA3 { set; get; } // 
            public string P_H_AREA4 { set; get; } // 
            public string P_RS_ROT { set; get; } // 
            public string P_RS_MAX { set; get; } // 
            public string P_RS_DEG { set; get; } // 
            public string P_RS_AREA1 { set; get; } // 
            public string P_RS_AREA2 { set; get; } // 
            public string P_RS_AREA3 { set; get; } // 
            public string P_RS_AREA4 { set; get; } // 
            public string P_F_ROT { set; get; } // 
            public string P_F_MAX { set; get; } // 
            public string P_F_DEG { set; get; } // 
            public string P_F_AREA1 { set; get; } // 
            public string P_F_AREA2 { set; get; } // 
            public string P_F_AREA3 { set; get; } // 
            public string P_F_AREA4 { set; get; } // 
            //frank 导联
            public string P_Frank_Heartbeat { set; get; } //  心率
            public string P_Frank_Druation { set; get; } //  房波时限
            public string P_Frank_Morphology { set; get; } //  形态被描述
            public string P_Frank_Conclusion { set; get; } //  结论
            //Wilson 导联
            public string P_Wilson_Heartbeat { set; get; } //  心率
            public string P_Wilson_Druation { set; get; } //  房波时限
            public string P_Wilson_RA_LA { set; get; } //  右左房时限
            public string P_Wilson_LA { set; get; } //  左房时限
            public string P_Wilson_PR { set; get; } //   pr间期
            public string P_Wilson_LA_V { set; get; } //   LAV间期
            public string P_Wilson_PStartLead { set; get; } //  p波起点最早导联
            public string P_Wilson_LA_PEndLead { set; get; } //   p波结束最晚导联
            public string P_Wilson_Conclusion { set; get; } //  结论
            public string PrintTime { set; get; } //  时间


            public string Fastreport_demo { set; get; } //   fastreport 模板路径
            public string Report_file { set; get; } // 生成报告的路径 


        }

        private void showPrintView()
        {
            Report report = new Report();
            // load the existing report
            report.Load(para.Fastreport_demo);
            // register the dataset
            //report.RegisterData(FDataSet);

            var newtype = para.GetType();
            foreach (var item in newtype.GetProperties())
            {
                string name = item.Name;
                string value = item.GetValue(para, null).ToString();
                report.SetParameterValue(name, value);
                if (name == "doctorsign")
                {
                    if (File.Exists(value))
                    {
                        Bitmap digitalsign = new System.Drawing.Bitmap(value);
                        FastReport.PictureObject imgobj1 = report.FindObject("docsign") as FastReport.PictureObject;
                        imgobj1.Image = digitalsign;
                        report.SetParameterValue("doctor", "");
                    }
                }

            }
            // run the report
            report.Show();
            // free resources used by report
            report.Dispose();
        }


        private void SavePDF()
        {
            try
            {
                Report report = new Report();
                // load the existing report
                report.Load(para.Fastreport_demo);
                
                report.Prepare();
                var newtype = para.GetType();
                foreach (var item in newtype.GetProperties())
                {
                    string name = item.Name;
                    string value = item.GetValue(para, null).ToString();
                    report.SetParameterValue(name, value);

                }
                string path = para.Report_file;
                PDFExport pdf = new PDFExport();
                report.Export(pdf, path);
                // free resources used by report
                report.Dispose();
                MessageBox.Show(path + "保存成功！");
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }

        }
        

        private void Form1_Load_1(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
